<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>抢筹</title>
		<link href="css/text.css" rel="stylesheet" type="text/css" />
		<style type="text/css">				
			p {margin:5px}
			ul {margin:1px}			
			h4 {margin:1px}	
			.fixedBox{
				border: 1px solid #555555;				
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 10px;     
				height: 10px;				
			}
			table{
				width:480px; 
				border-collapse:collapse; 
				text-align:center;
			}
			td,th{border:1px solid #696969}	
		</style>
	</head>
	<body>
		<div id="towrite" class="fixedBox"></div>	
		<script type="text/javascript" src="js/json2html.js"></script>
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script type="text/javascript" src="DatePicker/WdatePicker.js"> </script> 
		<script>
			var bkcolor = $("body").css("backgroundColor")
			var day = new Date();
			var tt = "";
			var dt = "";
			var k = 0;
			window.onload = function() {						
				mymonth = day.getMonth()+1;
				myday = day.getDate();
				if(mymonth >= 10){mymonth = mymonth;}else{mymonth = "0" + mymonth;}
				if(myday >= 10){myday = myday;}else{myday = "0" + myday;}
				tt = day.getFullYear() + "-" + mymonth + "-" + myday;
				//dt = tt;
				tradeday();
				getSource("0","1");
				changeStyle();
			}
						
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
					getSource(t,px);					
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {
				time_range("9:00","10:00")									
				},
				60000);
			})
			
			function changeStyle() {
				var obj1 = document.getElementById("towrite");				
				obj1.style.left= "0px"; 				
				obj1.style.width= "500px";	
				//obj1.style.height= "620px";		
				obj1.style.fontSize="12px";				  			 
			}	
			
			Date.prototype.Format = function (fmt) {
				var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
				};
				if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function getLocalTime(nS) {			          
			return new Date(parseInt(nS) * 1000).Format("hh:mm:ss")}						
			
			function keepTwoDecimal(num) {  
               var result = parseFloat(num);  
               if (isNaN(result)) {  
                   //alert('传递参数错误，请检查！');  
                   return false;  
               }  
               result = Math.round(num * 100) / 100;  
               return result;  
			};
			
			function NumberTransform(num) { 
				var result = num;
				if(Math.abs(num) > 100000000){
					result = num / 100000000;
					result = result.toFixed(2) + "亿";
				}else if(Math.abs(num) > 10000){
					result = num/10000;
					result = result.toFixed(0) + "万";
				}				   
				return result;  
			};	

			var trid = "";
			function changeCss(obj,hid,id){						
				var tlist = document.getElementById(id).getElementsByTagName("tr");					
				for(var i = 0; i < tlist.length; i++){														
					tlist[i].style.backgroundColor = bkcolor; 					
				}
				trid = hid;
				var tr = document.getElementById(hid);				
				tr.style.backgroundColor = "#483D8B";						
			}
			
			function hover(){  //鼠标经过
				var rows = document.getElementsByTagName('tr');
				for(var i=0;i<rows.length;i++){
					rows[i].onmouseover=function(){						
						if(this.id != ""){  //过滤表头
							this.style.background="#800000";
						}
					}
					rows[i].onmouseout=function(){
						if(this.id != ""){  //过滤表头
							if(this.id != trid){  //过滤选中行
								this.style.background=bkcolor;
							}else{
								this.style.background="#483D8B";
							}
						}						
					}					
				}
			}			
			
			function cDayFunc(dp) { 
				var date = dp.cal.getNewDateStr();            
				BookingRmk(date);//调用其它函数并带上参数                
			}
			
			function BookingRmk(date){         
				tt = date;				
				var date1 = new Date(tt);
				var date2 = new Date(dt);				
				if(date1.getTime() <= date2.getTime()){			
					if(tt == dt){
						pn = -1;						
					}
					tradeday();
					if(dt == tt){
						k = 0;
					}else{
						k = 1;
					}
					getSource(t,px);
				}else{
					tt = dt;
				}				
			}
			
			function prev(){  //前一天
				pn = -1;
				var day = new Date(tt);	
				day.setTime(day.getTime() - 24*60*60*1000);	
				tt = day.Format("yyyy-MM-dd");
				BookingRmk(tt);				
			}
			
			var k = 0;
			function next(){  //后一天
				k++;
				pn = 1;
				var day = new Date(tt);	
				day.setTime(day.getTime() + 24*60*60*1000);	
				tt = day.Format("yyyy-MM-dd");
				BookingRmk(tt);							
			}
			
			var pn = -1;			
			function tradeday() {  //判断最后交易日
				var day = new Date(tt);							
				for (i = 1; i < 100; i++){	//节假日																										
					if(i > 1){
						day.setTime(day.getTime() + pn*24*60*60*1000);							
					}	
					tt = day.Format("yyyy-MM-dd");					
					//alert(tt);
					var url = "http://push2ex.eastmoney.com/getTopicQSPool?cb=callbackdata3686089&ut=7eea3edcaed734bea9cbfc24409ed989&dpt=wz.ztzt&Pageindex=0&pagesize=100&sort=zdp%3Adesc&date=" + tt.replace(/-/g,""); 
					var res = "";
					$.ajax({
						type: "get",
						url: url,
						dataType: "text",
						async : false,
						cache:false,
						timeout:3000,						
						success: function(data) {
							//alert(data)						
							//document.write(data);	
							res = data;							
						}
					});
					if(res.indexOf('"pool":[]') == -1){
						if(dt == ""){
							var rq = res.match(/"qdate":(\S*),"pool"/)[1];
							tt = rq.substr(0,4) + "-" + rq.substr(4,2) + "-" + rq.substr(6,2);
							dt = tt;												
						}
						break;
					}					
				}
			}	
			
			var t = "0";
			var px = "1";
			var gl = "抢筹委托金额";  //高亮
			var dms = "";
			var bj = [];
			function getSource(ss,pp) {
				t = ss;
				px = pp;
				var zp = "<a href='javascript:void(0)' id='bt1' onclick='getSource(" + "\"0\"" + "," + px + ")'>" + "早盘抢筹" + "</a>";
				var wp = "<a href='javascript:void(0)' id='bt2' onclick='getSource(" + "\"1\"" + "," + px + ")'>" + "尾盘抢筹" + "</a>";	
				
				div1 = document.getElementById('towrite');
				div1.innerHTML = "<font size='2' style='margin-top:-5px;'>" + zp + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + wp + "</font>";
				div1.innerHTML += '<input style="margin-left:20px;width: 100px;" type="text" id="PriceDate" name="PriceDate" class="Wdate" onFocus="WdatePicker({ onpicking: cDayFunc})" />'
				var back = '<a href="javascript:prev();"  style="padding-left:10px;font-size:16px;color:#fff;">←</a>';				
				var forward = '<a href="javascript:next();"  style="font-size:16px;color:#fff;">→</a>';
				div1.innerHTML += back + forward ;
				
				if(t == "0"){
					aObj = document.getElementById('bt1');				
					aObj.style.color = "#FFA07A";
				}
				
				if(t == "1"){
					aObj = document.getElementById('bt2');				
					aObj.style.color = "#FFA07A"; 
				}
				
				var url = "http://excalc.icfqs.com:7616/TQLEX?Entry=HQServ.hq_nlp"; 	                			
				//createXmlHttp();
				//早盘				
				var array11 = '[{"funcId":20,"offset":0,"count":50,"sort":' + px + ',"period":0,"Token":"6679f5cadca97d68245a086793fc1bfc0a50b487487c812f","modname":"JJQC"}]';
				array11 = eval(array11);
				
				var array12 = '[{"funcId":20,"offset":0,"count":50,"sort":' + px + ',"period":0,"Token":"6679f5cadca97d68245a086793fc1bfc0a50b487487c812f","modname":"JJQC","date":Number(tt.replace(/-/g,""))}]';
				array12 = eval(array12);
							
				
				//尾盘				
				var array21 = '[{"funcId":20,"offset":0,"count":50,"sort":' + px + ',"period":1,"Token":"6679f5cadca97d68245a086793fc1bfc0a50b487487c812f","modname":"JJQC"}]';
				array21 = eval(array21);	
				
				var array22 = '[{"funcId":20,"offset":0,"count":50,"sort":' + px + ',"period":1,"Token":"6679f5cadca97d68245a086793fc1bfc0a50b487487c812f","modname":"JJQC","date":Number(tt.replace(/-/g,""))}]';
				array22 = eval(array22);	
				
				var array1 = [];
				var array2 = [];
				if(k == 0){
					array1 = array11;
					array2 = array21;
				}else{
					array1 = array12;
					array2 = array22;
				}
				
				var array = [];
				if(ss == "0"){	
					array = array1;						
				}
				if(ss == "1"){											
					array = array2;	
				}		
				
				var res = [];
				$.ajax({
					type: "post",
					url: url,
					data : JSON.stringify(array),					
					headers: {"User-Agent":"TdxW"},
					dataType: "json",
					async : false,
					cache:false,
					timeout:3000,					
					success: function(data) {
						//alert(JSON.stringify(data))						
						//document.write(JSON.stringify(data));							
						for (i = 0; i < data.datas.length; i++){
							var aa = data.datas[i];
							var qczb = 0;
							if(aa[4] != 0){						
								qczb = (aa[7] / aa[4]) * 100;
							}
							res.push({"code":aa[0],"name":aa[1],"kpje":NumberTransform(aa[4]),"fd":keepTwoDecimal(aa[5]*100),"wtje":NumberTransform(aa[6]),"cjje":NumberTransform(aa[7]),"qczb":keepTwoDecimal(qczb)});							
						}									
					}
				});	
				
				if(res.length > 0){	
				
					var json = res;
					//创建table					
					var table=document.createElement("table");										
					var thead=document.createElement("thead");
					table.appendChild(thead);					
					var tr=document.createElement("tr");
					thead.appendChild(tr);					
					
					var m = 0;
					var n = 0;
					var b = 0;
					for(var key in json[0]){  //表头
						key = key.replace('code','代码').replace('name','名称').replace('fd','抢筹幅度%').replace('wtje','抢筹委托金额').replace('cjje','抢筹成交金额').replace('qczb','抢筹占比%');							
						var ks = "";
						if(t == "0"){
							key = key.replace('kpje','开盘金额');															
						}else{
							key = key.replace('kpje','收盘金额')															
						}									
						var th=document.createElement("th");						
						th.innerHTML=key;						
						tr.appendChild(th);
						if(gl == "开盘金额" && t == 1){
							gl = "收盘金额"
						}else if(gl == "收盘金额" && t == 0){
							gl = "开盘金额"
						}
						if(gl == key){
							n = m;
							th.style.color = "#FF6633";	
						}							
						m++;
					}
										
					var tbody=document.createElement("tbody");					
					table.appendChild(tbody);
					
					//遍历json
					for(var i = 0; i < json.length; i++){					
						var tr=document.createElement("tr");						
						var p = 1;
						var dm = "";
						var mc = "";
						for(var key in json[i]){ //行元素遍历						
							var td=document.createElement("td");							
							
							var rc = json[i][key];
							if(p == 1){  //代码列
								dm = rc;
								tr.id = dm;
							}else if(p == 2){  //名称列
								mc = rc;								
							}					
							td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "' onclick='changeCss(this,\"" + dm + "\",\"gg\")'>" + rc + "</a>";
						
							if(p == n + 1){							
								//td.style.color = "#FF6633";	
								td.getElementsByTagName("a")[0].style.color = "#FF6633";									
							}								
							tr.appendChild(td);
							p++;
						}						
						//if(mc.indexOf("ST") == -1 && dm.substr(0,3) != "688"){		//过滤ST、科创板
						if(mc.indexOf("ST") == -1){		//过滤ST					
							tbody.appendChild(tr);
						}	
					}					
					var div = document.getElementById("towrite");									
					div.appendChild(table);
					table.id = "gg";
					table.style.marginTop = "5px";
					hover();
					makeSortable(table);
				}else{						
					div1.innerHTML += "<br>暂无数据";
				}
				document.getElementById("PriceDate").value = tt;
			}
			
			function makeSortable(table) {
				var headers=table.getElementsByTagName("th");
				for(var i = 0; i < headers.length; i++){				
					(function(n){
						var flag=false;
						headers[n].onclick = function(){							
							if(n > 1 && n < 6){
								gl = headers[n].textContent;
								var p = 0;
								if(n == 2){  //开盘金额	
									p = 3;
								}else if(n == 3){  //抢筹幅度
									p = 4;
								}else if(n == 4){  //抢筹委托金额
									p = 1;
								}else if(n == 5){  //抢筹成交金额
									p = 2;
								}
								getSource(t , p);
							}else{
								showCol(n,bj);		//高亮
								var tbody=table.tBodies[0];
								var rows=tbody.getElementsByTagName("tr");
								rows=Array.prototype.slice.call(rows,0);

								//基于第n个<td>元素的值对行排序
								rows.sort(function(row1,row2){
									headtxt = headers[n].textContent;								
									var cell1=row1.getElementsByTagName("td")[n];//获得第n个单元格
									var cell2=row2.getElementsByTagName("td")[n];																
									var val1=cell1.textContent||cell1.innerText;//获得文本内容
									var val2=cell2.textContent||cell2.innerText;
									if(headtxt == "抢筹占比%"){									
										val1 = parseFloat(val1);										
										val2 = parseFloat(val2);
									}
									if(val1<val2){
										return -1;
									}else if(val1>val2){
										return 1;
									}else{
										return 0;
									}
									
								});
								if(flag){
									rows.reverse();								
								}
								
								for(var i=0;i<rows.length;i++){
									tbody.appendChild(rows[i]);
								}

								flag=!flag;
							
							}
							
						}
					}(i));
				}
			}
			
			function showCol(iCol,arr){	 //(选中列，标记列)	
				var oTable = document.getElementsByTagName('table')[0];						
				//表头
				var th = oTable.getElementsByTagName("th");						
				for (var i=0;i < th.length; i++){	    //列遍历
					th[i].style.color = "#E5E5E5";      //还原					
					if(arr.length > 0 && arr.indexOf(i) > -1){  //BJ列高亮
						th[i].style.color = "#FFA500";
					}
					th[iCol].style.color = "#FF6633";	//选中				
				}
				
				var tr = oTable.getElementsByTagName("tr");	
				for (var i=1;i < tr.length; i++){	//行遍历
					var otagsA = tr[i].getElementsByTagName("a");
					for (var j=0;j < otagsA.length; j++){	//列遍历
						otagsA[j].style.color = "#E5E5E5";	//还原																
						if(arr.length > 0 && arr.indexOf(j) > -1){  //BJ列高亮
							otagsA[j].style.color = "#FFA500";
						}
					}
					var value = otagsA[iCol].innerText.replace(/(万)|(亿)/g,'');
					if(!isNaN(parseFloat(value)) && isFinite(value)){  //判断列是否为数字
						if(parseFloat(value) >= 0){
							otagsA[iCol].style.color = "#FF6633";     //正负颜色
						}else{
							otagsA[iCol].style.color = "#00FF00";
						}
					}else{
						otagsA[iCol].style.color = "#FF6633";
					}					
				}					
			}	
		</script>	
	</body>
</html>